﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 美妆产品管理系统

{
    public partial class Check_Order : Form
    {
        public Check_Order()
        {
            InitializeComponent();
        }
        SqlDataAdapter daOrder;
        DataSet ds = new DataSet();
        void init()
        {
            DB.GetCn();
            string str = "select * from Orders";
            daOrder = new SqlDataAdapter(str, DB.cn);
            daOrder.Fill(ds, "order_info");
            DB.cn.Close();
        }


        void showAll()
        {
            init();
            showXz();
        }

        void showXz()
        {
            DataGridViewCheckBoxColumn acCode = new DataGridViewCheckBoxColumn();
            acCode.Name = "acCode";
            acCode.HeaderText = "选择";
            dgvOrder.Columns.Add(acCode);
        }

        private void dgvOrder_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvOrder.Rows.Count > 0)
            {
                for(int i = 0; i < dgvOrder.Rows.Count;i++)
                {
                    DataGridViewCheckBoxCell ck = dgvOrder.Rows[i].Cells["acCode"] as DataGridViewCheckBoxCell;
                    if(i != e.RowIndex)
                    {
                        ck.Value = false;
                    }
                    else
                    {
                        ck.Value = true;
                    }
                }
            }
        }

        private void Check_Order_Load(object sender, EventArgs e)
        {
            if (Login.Dflag == false)
            {
                MessageBox.Show("您没有权限，请联系销售部员工进行操作");
            }
            else
            {
                showAll();
                DataView dvOrder = new DataView(ds.Tables["order_info"]);
                dgvOrder.DataSource = dvOrder;
            }
        }

        private void btnok_Click(object sender, EventArgs e)
        {
            int s = dgvOrder.Rows.Count;
            for(int i = 0; i < dgvOrder.Rows.Count; i++) 
            {
               
               if (dgvOrder.Rows[i].Cells["acCode"].EditedFormattedValue.ToString()== "True")
                {
                    dgvOrder.Rows[i].Cells["OStatus"].Value = "已审核";
                    if (string.IsNullOrEmpty(dgvOrder.Rows[i].Cells["EmployeeID"].Value?.ToString()))
                    {
                        dgvOrder.Rows[i].Cells["EmployeeID"].Value = Login.Employee_ID;
                    }
                    UpdateDB();
                }
                else
                {
                    s = s - 1;
                }
            }
            if (s == 0)
            {
                MessageBox.Show("请选择需要审核的订单");
            }
        }

        void UpdateDB()//更新数据源
        {
            try
            {
                SqlCommandBuilder dbOrder = new SqlCommandBuilder(daOrder);
                daOrder.Update(ds, "Order_info");
            }
            catch(SqlException ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

        private void btnCance_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
